package com.example.shangjiqi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.shangjiqi.R;

public class HomeActivity extends AppCompatActivity {
    private static final String TAG = "HomeActivity";
    private static final String CURRENT_FRAGMENT_TAG = "current_fragment";

    private Button btnNavHome;
    private Button btnNavMine;
    private String currentFragmentTag = "home";
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_home);

        btnNavHome = findViewById(R.id.btn_nav_home);
        btnNavMine = findViewById(R.id.btn_nav_mine);

        sharedPreferences = getSharedPreferences("UserData", MODE_PRIVATE);

        // 接收登录页传递的用户名并保存
        Intent intent = getIntent();
        String username = intent.getStringExtra("username");
        if (username != null) {
            sharedPreferences.edit().putString("username", username).apply();
        }

        // 处理屏幕旋转时的Fragment恢复
        if (savedInstanceState != null) {
            currentFragmentTag = savedInstanceState.getString(CURRENT_FRAGMENT_TAG, "home");
            // Fragment会自动恢复，不需要重新创建
        } else {
            // 首次创建时，显示HomeFragment
            loadFragment(new HomeFragment(), "home");
        }

        // 更新按钮状态
        updateNavigationButtons();

        // 首页按钮点击事件
        btnNavHome.setOnClickListener(v -> {
            if (!currentFragmentTag.equals("home")) {
                switchFragment("home");
            }
        });

        // 我的按钮点击事件
        btnNavMine.setOnClickListener(v -> {
            if (!currentFragmentTag.equals("mine")) {
                switchFragment("mine");
            }
        });
    }

    private void loadFragment(Fragment fragment, String tag) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment, tag);
        transaction.commit();
        currentFragmentTag = tag;
        updateNavigationButtons();
    }

    private void switchFragment(String tag) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag(tag);

        if (fragment == null) {
            // 如果Fragment不存在，创建新的
            if (tag.equals("home")) {
                fragment = new HomeFragment();
            } else if (tag.equals("mine")) {
                fragment = new MineFragment();
            }
        }

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment, tag);
        transaction.commit();
        currentFragmentTag = tag;
        updateNavigationButtons();
    }

    private void updateNavigationButtons() {
        // 更新按钮选中状态（可以通过背景色或文字颜色区分）
        if (currentFragmentTag.equals("home")) {
            btnNavHome.setAlpha(1.0f);
            btnNavMine.setAlpha(0.5f);
        } else {
            btnNavHome.setAlpha(0.5f);
            btnNavMine.setAlpha(1.0f);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // 保存当前Fragment标签，用于屏幕旋转时恢复
        outState.putString(CURRENT_FRAGMENT_TAG, currentFragmentTag);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }
}